package com.dd.ent.car.ui.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.dd.ent.car.common.util.DipPixelUtil;
import com.pp.ent.base.log.MLog;

/**
 * Created by dengqu on 2016/8/29.
 */
public class RefreshPromptView extends TextView {
    private static final int ANIMATION_DURATION = 500;
    private static final int ANIMATION_DELAY_DURATION = 1000;
    private final static String TAG = "RefreshPromptView";
    private AnimatorSet animatorSet;
    private ObjectAnimator show;
    private ObjectAnimator hide;

    public RefreshPromptView(Context context) {
        super(context);
    }

    public RefreshPromptView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void show(int refreshSize) {
        MLog.debug(TAG, "show refreshSize=" + refreshSize);
        String promptText = refreshSize + " 条内容更新";
        String refreshSizeText = String.valueOf(refreshSize);
        int index = promptText.indexOf(refreshSizeText);
        if (index < 0) {
            return;
        }
        SpannableStringBuilder style = new SpannableStringBuilder(promptText);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#1294f6")), index, index + refreshSizeText.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new AbsoluteSizeSpan(DipPixelUtil.sp2pix(14, getResources().getDisplayMetrics().density)), index, index + refreshSizeText.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        setText(style);
        if (animatorSet != null && animatorSet.isRunning()) {
            animatorSet.cancel();
        }
        if (animatorSet == null || show == null || hide == null) {
            animatorSet = new AnimatorSet();
            show = ObjectAnimator.ofFloat(this, View.TRANSLATION_Y, -getHeight(), 0);
            show.setDuration(ANIMATION_DURATION);
            hide = ObjectAnimator.ofFloat(this, View.TRANSLATION_Y, 0, -getHeight());
            hide.setDuration(ANIMATION_DURATION);
            hide.setStartDelay(ANIMATION_DELAY_DURATION);
            animatorSet.playSequentially(show, hide);
        }
        animatorSet.start();
    }


}
